import { User } from "../../../../modeal/user.js"
const user = new User()
Page({

  data: {
    tabs: [
      { id: 1, name: "堂食" },
      { id: 2, name: "自提" },
      { id: 3, name: "外卖" }
    ],
    page: 1,
    currentIndex: 1,
    orderList: [],
    statusText: "您没有当前订单"
  },

  onLoad: function (options) { },
  onReady: function () { },
  onShow: function () {
    this.getOrderList() //加载订单列表
  },
  onHide: function () {
    this.setData({
      orderList: [],
      page: 1,
      currentIndex: 1
    })
  },
  onReachBottom: function () {
    this.getOrderList();
  },

  onPullDownRefresh: function () {
    this.init();
    this.getOrderList()
  },


  // init
  init () {
    this.setData({ orderList: [], page: 1 })
  },

  // tabs切换
  tabClick (e) {
    let currentIndex = e.currentTarget.dataset.id;
    this.init()
    this.setData({ currentIndex })
    this.getOrderList()
  },

  // 加载订单列表
  getOrderList () {
    let page = this.data.page;
    let type = this.data.currentIndex; //订单类型
    user.getOrderList(type, page).then(res => {
      if (res.length >= 1) {
        this.setData({
          orderList: this.data.orderList.concat(res),
          page: page + 1,
          isMore: true,
        })
        res.length < 10 ? this.setData({ isMore: false }) : ''
      }
    })

  },

})